import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * Animator组件
 */
export default class AnimatorComponent {
    private static instance: AnimatorComponent = new AnimatorComponent();
    /**
     * Animator组件
     */
    public static get Default(): AnimatorComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    /**
     * 播放动画
     * @param instanceId GameObject实例Id
     * @param state Animator状态
     * @param duration 时长
     * @returns 是否播放成功
     */
    public async PlayAsync(instanceId: number, state: string, duration: number): Promise<boolean> {
        if (state == '') {
            window.Logger.Warning(`动画状态为空`);
            return false;
        }
        const response: ClientMessage.PlayAnimatorResponse = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.PlayAnimatorRequest(instanceId, state, duration));
        return response.Error == 0;
    }
}